package com.eu.ebpm.engine.express;

import java.util.HashMap;
import java.util.Map;

import com.eu.ebpm.engine.express.validator.BooleanTypeValidator;
import com.eu.ebpm.engine.express.validator.NumberTypeValidator;
import com.eu.ebpm.engine.express.validator.StringTypeValidator;
import com.eu.ebpm.engine.express.validator.TypeValidator;

public enum VariableType {
	
	NUMBER("数字类型"),
	
	STRING("字符类型"),
	
	BOOLEAN("布尔类型"),
	
	OBJECT("对象类型");
	
	private String desc;
	
	VariableType(String desc) {
		this.desc = desc;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}
	
	public static VariableType typeOf(Object value) {
		
		if(value instanceof Short || value instanceof Integer || value instanceof Long || value instanceof Double) return VariableType.NUMBER;
		
		else if(value instanceof String) return VariableType.STRING;
		
		else if(value instanceof Boolean) return VariableType.BOOLEAN;
		
		else return VariableType.OBJECT;
		
	}
	
	public TypeValidator<?> getParser() {
		return parsers.get(this);
	}
	
	@SuppressWarnings("unchecked")
	public <T> T transform(Object value) {
		return (T) getParser().transform(value);
	}
	
	public static Map<VariableType, TypeValidator<?>> parsers;
	
	static {
		
		parsers = new HashMap<VariableType, TypeValidator<?>>();
		
		parsers.put(VariableType.STRING, new StringTypeValidator());
		
		parsers.put(VariableType.NUMBER, new NumberTypeValidator());
		
		parsers.put(VariableType.BOOLEAN, new BooleanTypeValidator());
		
	}
	

}
